package com.pwc.conversionTool.biz;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.googlecode.objectify.Key;
import com.pwc.conversionTool.dao.DataAdaptorDao;
import com.pwc.conversionTool.dataAdaptor.CsvUtil;
import com.pwc.conversionTool.dataAdaptor.ExcelUtil;
import com.pwc.conversionTool.entity.Account;
import com.pwc.conversionTool.entity.CsvDataType;
import com.pwc.conversionTool.entity.DataTemplate;
import com.pwc.conversionTool.entity.ExcelDataType;
import com.pwc.conversionTool.entity.Formula;
import com.sun.corba.se.spi.orb.StringPair;

public class DataAdaptorBiz {

	private DataAdaptorDao dataAdaptorDao = new DataAdaptorDao();

	public DataTemplate saveDataAdaptor(DataTemplate dataAdaptor) {
		dataAdaptorDao.saveDataAdaptor(dataAdaptor);
		return dataAdaptor;

	}

	/**
	 * 
	 * @param lineSeparator
	 * @param columnSepatator
	 * @param file
	 * @throws IOException
	 */
	public CsvDataType saveCsvType(String lineSeparator, String columnSepatator, InputStream in, Long id, String adaptorName,
			String description) throws IOException {

		CsvUtil helper = null;
		String[] header = null;
		CsvDataType csvDataType = null;

		DataTemplate oldDataAdaptor = dataAdaptorDao.getDataAdaptorById(id);

		CsvDataType oldcsvDataType = dataAdaptorDao.getCsvDataTypeByDataTemplate(oldDataAdaptor);

		if (oldcsvDataType != null) {
			dataAdaptorDao.deleteCsvDataType(oldcsvDataType);
		}

		if (lineSeparator != null && !lineSeparator.isEmpty() && columnSepatator != null && !columnSepatator.isEmpty()) {

			helper = new CsvUtil(in, lineSeparator.charAt(0), columnSepatator.charAt(0));
			header = helper.getHeader();
			csvDataType = new CsvDataType(lineSeparator, columnSepatator, oldDataAdaptor.getKey());

		} else {
			helper = new CsvUtil(in);
			header = helper.getHeader();
			csvDataType = new CsvDataType(oldDataAdaptor.getKey());
		}

		dataAdaptorDao.saveCsvDataType(csvDataType);

		oldDataAdaptor.setHeader(header);

		oldDataAdaptor.setAdaptorName(adaptorName);

		oldDataAdaptor.setDescription(description);

		dataAdaptorDao.saveDataAdaptor(oldDataAdaptor);

		return csvDataType;
	}

	public List<DataTemplate> getDataAdaptorsByAccount(Account account) {
		return dataAdaptorDao.getDataAdaptorsByAccount(account);
	}

	public DataTemplate getDataAdaptorById(Long id) {
		return dataAdaptorDao.getDataAdaptorById(id);
	}

	public CsvDataType getCsvDataTypeById(Long id) {
		return dataAdaptorDao.getCsvDataTypeById(id);
	}

	// public CsvDataType getCsvDataTypeByDataTyeId(Long id)
	// {
	// return dataAdaptorDao.getCsvDataTypeByDataTypeId(id);
	// }

	public void deleteDataAdaptorById(Long id) {
		DataTemplate dataAdaptor = dataAdaptorDao.getDataAdaptorById(id);

		dataAdaptorDao.deleteDataAdaptor(id);
		// delete the other type of file

		dataAdaptorDao.deleteCsvDataType(dataAdaptor);

	}

	public CsvDataType getCsvDataTypeByDataTemplate(DataTemplate dataTemplate) {
		return dataAdaptorDao.getCsvDataTypeByDataTemplate(dataTemplate);
	}

	public ExcelDataType saveExcelType(String fileName, String sheetName, InputStream in, Long id, String adaptorName, String description)
			throws IOException {

		ExcelUtil excelUtil = null;

		DataTemplate oldDataAdaptor = dataAdaptorDao.getDataAdaptorById(id);

		ExcelDataType oldExcelDataType = dataAdaptorDao.getExcelDataType(oldDataAdaptor);

		if (oldExcelDataType == null) {
			oldExcelDataType = new ExcelDataType(sheetName, oldDataAdaptor.getKey());

		} else {
			oldExcelDataType.setSheetName(sheetName);
			oldExcelDataType.setDataTemplate(oldDataAdaptor.getKey());
		}

		dataAdaptorDao.saveExcelDataType(oldExcelDataType);

		excelUtil = new ExcelUtil(fileName, in, sheetName);

		String[] header = excelUtil.getHeader();

		oldDataAdaptor.setAdaptorName(adaptorName);

		oldDataAdaptor.setDescription(description);

		oldDataAdaptor.setHeader(header);

		dataAdaptorDao.saveDataAdaptor(oldDataAdaptor);

		return oldExcelDataType;
	}

	public ExcelDataType getExcelDataType(DataTemplate dataTemplate) {

		return dataAdaptorDao.getExcelDataType(dataTemplate);
	}

	public void saveFormula(Formula formula) {
		
		DataTemplate dataTemplate = dataAdaptorDao.getDataAdaptorById(formula.getDataTemplateId());

		formula.setDataTemplateKey(dataTemplate.getKey());

		dataAdaptorDao.saveFormula(formula);

	}

	public Formula getFormula(DataTemplate dataTemplate) {		

		return dataAdaptorDao.getFormulaByDataTemplate(dataTemplate.getKey());
	}
	
	public Formula getFormula(Long dataTemplateId) {		

		DataTemplate dataTemplate=dataAdaptorDao.getDataAdaptorById(dataTemplateId); 
		
		return dataAdaptorDao.getFormulaByDataTemplate(dataTemplate.getKey());
	}
	
	

}
